package com.guaju.yueshangLife.adapter;

import android.app.Activity;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.guaju.yueshangLife.R;
import com.guaju.yueshangLife.databinding.FooterHomeItemBinding;
import com.guaju.yueshangLife.databinding.ItemCancledBinding;
import com.guaju.yueshangLife.databinding.ItemSelectCancledBinding;
import com.guaju.yueshangLife.databinding.ItemSelectUncommentBinding;
import com.guaju.yueshangLife.databinding.ItemSelectUnpayBinding;
import com.guaju.yueshangLife.databinding.ItemSelectUnreceivedBinding;
import com.guaju.yueshangLife.databinding.ItemUncommentBinding;
import com.guaju.yueshangLife.databinding.ItemUnpayBinding;
import com.guaju.yueshangLife.databinding.ItemUnreceivedBinding;

import java.util.List;

public class SelectOrderListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    Context context;
    List list;
    private int status = 1;

    public static final int TYPE_UNPAY = 9901;
    public static final int TYPE_UNRECEIVED = 9902;
    public static final int TYPE_UNCOMMENT = 9903;
    public static final int TYPE_CANCLED = 9904;
    public static final int LOAD_MORE = 0;
    public static final int LOAD_PULL_TO = 1;
    public static final int LOAD_NONE = 2;
    public static final int LOAD_END = 3;
    private static final int TYPE_FOOTER = -2;
    private static final int TYPE_CONTENT = -4;

    Activity activity;
    private final LayoutInflater layoutInflater;
    private View view;

    public SelectOrderListAdapter(Context context, List list) {
        this.context = context;
        this.list = list;
        activity = (Activity) context;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int type) {

        //选择类型
        RecyclerView.ViewHolder result = null;
        switch (type) {
            case TYPE_UNPAY:
                ItemSelectUnpayBinding unpayBinding = DataBindingUtil.inflate(layoutInflater, R.layout.item_select_unpay, parent, false);
                result = new UnPayHolder(unpayBinding.getRoot());
                break;
            case TYPE_UNRECEIVED:
                ItemSelectUnreceivedBinding unreceivedBinding = DataBindingUtil.inflate(layoutInflater, R.layout.item_select_unreceived, parent, false);
                result = new UnReceivedHolder(unreceivedBinding.getRoot());
                break;
            case TYPE_UNCOMMENT:
                ItemSelectUncommentBinding uncommentBinding = DataBindingUtil.inflate(layoutInflater, R.layout.item_select_uncomment, parent, false);
                result = new UnCommentHolder(uncommentBinding.getRoot());
                break;
            case TYPE_CANCLED:
                ItemSelectCancledBinding cancledBinding = DataBindingUtil.inflate(layoutInflater, R.layout.item_select_cancled, parent, false);
                result = new CancledHolder(cancledBinding.getRoot());
                break;
            case TYPE_FOOTER:
                FooterHomeItemBinding mBindFooter = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.footer_home_item, parent, false);
                result = new FooterViewHolder(mBindFooter.getRoot());
                break;
        }
        return result;
    }

    //绑定
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder == null) {
            return;
        }
        if (holder instanceof FooterViewHolder) {
            FooterViewHolder footerViewHolder = (FooterViewHolder) holder;
            footerViewHolder.bindItem();
        } else if (holder instanceof UnPayHolder) {
            UnPayHolder h = (UnPayHolder) holder;
        } else if (holder instanceof UnReceivedHolder) {
            UnReceivedHolder h = (UnReceivedHolder) holder;

        } else if (holder instanceof UnReceivedHolder) {
            UnReceivedHolder h = (UnReceivedHolder) holder;

        } else if (holder instanceof UnCommentHolder) {
            UnCommentHolder h = (UnCommentHolder) holder;
        } else if (holder instanceof CancledHolder) {
            CancledHolder h = (CancledHolder) holder;
        }
    }

    //有多少个item？
    @Override
    public int getItemCount() {
        return 1 + list.size();
    }

    //item类型
    @Override
    public int getItemViewType(int position) {
        int type = TYPE_CONTENT;
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            Object o = list.get(position);
            if (o instanceof String) {
                String title = (String) o;
                type = TYPE_UNPAY;
            } else if (false) {
                type = TYPE_UNRECEIVED;
            } else if (false) {
                type = TYPE_CANCLED;
            } else if (false) {
                type = TYPE_UNCOMMENT;
            }
            return type;
        }
    }


    public interface OnItemClickListener {
        void onItemClick(View v, int position);

        void onItemLongClick(View v);
    }

    public SelectOrderListAdapter.OnItemClickListener mOnItemClickListener;//第二步：声明自定义的接口

    //第三步：定义方法并暴露给外面的调用者
    public void setOnItemClickListener(SelectOrderListAdapter.OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


    class UnPayHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        ItemSelectUnpayBinding unpayBinding;

        public UnPayHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);

        }

        public void setContent() {
            unpayBinding.tvSendOrder.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    activity.finish();
                }
            });

        }

        @Override
        public void onClick(View v) {
            activity.finish();

        }
    }

    class UnReceivedHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        ItemSelectUnreceivedBinding unreceivedBinding;

        public UnReceivedHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);

        }

        public void setContent() {
            unreceivedBinding.tvSendOrder.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    activity.finish();
                }
            });

        }

        @Override
        public void onClick(View v) {

            activity.finish();
        }
    }

    class UnCommentHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        ItemSelectUncommentBinding uncommentBinding;

        public UnCommentHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
        }

        public void setContent() {

            uncommentBinding.tvSendOrder.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    activity.finish();
                }
            });
        }

        @Override
        public void onClick(View v) {
            activity.finish();
        }
    }

    class CancledHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        ItemSelectCancledBinding cancledBinding;

        public CancledHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);

        }

        public void setContent() {
            cancledBinding.tvSendOrder.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    activity.finish();
                }
            });
        }

        @Override
        public void onClick(View v) {
            activity.finish();
        }
    }


    /**
     * footer view
     */
    private class FooterViewHolder extends RecyclerView.ViewHolder {

        FooterHomeItemBinding mBindFooter;

        FooterViewHolder(View itemView) {
            super(itemView);
            mBindFooter = DataBindingUtil.getBinding(itemView);
            mBindFooter.rlMore.setGravity(Gravity.CENTER);
        }

        private void bindItem() {
            switch (status) {
                case LOAD_MORE:
                    mBindFooter.progress.setVisibility(View.VISIBLE);
                    mBindFooter.tvLoadPrompt.setText("正在加载...");
                    itemView.setVisibility(View.VISIBLE);
                    mBindFooter.tvLoadPrompt.setVisibility(View.VISIBLE);
                    mBindFooter.homeBottom.setVisibility(View.INVISIBLE);
                    break;
                case LOAD_PULL_TO:
                    mBindFooter.progress.setVisibility(View.GONE);
                    mBindFooter.tvLoadPrompt.setText("上拉加载更多");
                    itemView.setVisibility(View.VISIBLE);
                    mBindFooter.tvLoadPrompt.setVisibility(View.VISIBLE);
                    mBindFooter.homeBottom.setVisibility(View.INVISIBLE);
                    break;
                case LOAD_NONE:
                    mBindFooter.progress.setVisibility(View.GONE);
                    mBindFooter.tvLoadPrompt.setText("暂无数据");
                    mBindFooter.tvLoadPrompt.setVisibility(View.VISIBLE);
                    mBindFooter.homeBottom.setVisibility(View.INVISIBLE);
                    break;
                case LOAD_END:
                    mBindFooter.progress.setVisibility(View.GONE);
                    mBindFooter.tvLoadPrompt.setVisibility(View.GONE);
                    mBindFooter.homeBottom.setVisibility(View.VISIBLE);
//                    itemView.setVisibility(View.GONE);
                default:
                    break;
            }
        }

    }

    /**
     * 这里规定 position =  getItemCount() - 1 时
     * 就为尾布局，设置为占满整屏幕宽
     * getItemCount() 改了 ，这里就不用改
     */
    private boolean isFooter(int position) {
        return position < getItemCount() && position >= getItemCount() - 1;
    }

    public void updateLoadStatus(int status) {
        this.status = status;
        notifyDataSetChanged();
    }

    public int getLoadStatus() {
        return this.status;
    }


}